//
//  ViewController.swift
//  YTImgeViewer
//
//  Created by flypigrmvb on 05/24/2018.
//  Copyright (c) 2018 flypigrmvb. All rights reserved.
//

import UIKit
import YTImgeViewer
import Kingfisher

class ViewController: UIViewController, YTImageDataProviderProtocol {
    
    lazy var images: [YTImage] = {
        var images = [YTImage]()
        
        var image = YTImage()
        image.thumbUrlString = "http://wx3.sinaimg.cn/thumbnail/bfc243a3gy1febm7nzbz7j20ib0iek5j.jpg"
        image.highUrlString = "http://wx3.sinaimg.cn/large/bfc243a3gy1febm7nzbz7j20ib0iek5j.jpg"
        images.append(image)
        
        image = YTImage()
        image.thumbUrlString = "http://wx1.sinaimg.cn/thumbnail/bfc243a3gy1febm7n9eorj20i60hsann.jpg"
        image.highUrlString = "http://wx1.sinaimg.cn/large/bfc243a3gy1febm7n9eorj20i60hsann.jpg"
        images.append(image)
        
        image = YTImage()
        image.thumbUrlString = "http://wx1.sinaimg.cn/thumbnail/bfc243a3gy1febm7orgqfj20i80ht15x.jpg"
        image.highUrlString = "http://wx1.sinaimg.cn/large/bfc243a3gy1febm7orgqfj20i80ht15x.jpg"
        images.append(image)
        
        return images
    }()

    @IBOutlet weak var imageView1: UIImageView!
    @IBOutlet weak var imageView2: UIImageView!
    @IBOutlet weak var imageView3: UIImageView!
    
    lazy var imageViews = [imageView1, imageView2, imageView3]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        for imageIndex in 0..<images.count {
            if imageIndex >= imageViews.count {
                break
            }
            let image = images[imageIndex]
            let imageView = imageViews[imageIndex]
            imageView?.kf.setImage(with: URL(string: image.thumbUrlString!))
            
            let singleTap = UITapGestureRecognizer(target: self, action: #selector(singleTap(gesture:)))
            imageView?.isUserInteractionEnabled = true
            imageView?.tag = imageIndex
            imageView?.addGestureRecognizer(singleTap)
        }
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK:- UIResponder

    @IBAction func onImageViewerBtnClick(_ sender: Any) {
        YTImageViewerController.show(self, images: images, currentImageIndex: 0, delegate: self)
    }
    
    @objc func singleTap(gesture: UITapGestureRecognizer) {
        if let tag = gesture.view?.tag {
            YTImageViewerController.show(self, images: images, currentImageIndex: tag, delegate: self)
        }
    }
    
    // MARK:- YTImageDataProviderProtocol
    func imgageViewer(_ imgageViewer: YTImageViewerController, sourceViewForCurImageIndex: Int) -> (UIView?, UIImage?) {
        if sourceViewForCurImageIndex >= imageViews.count {
            return (nil, nil)
        }
        guard let imageView = imageViews[sourceViewForCurImageIndex] else {
            return (nil, nil)
        }
        return (imageView, imageView.image)
    }
    
}

